package cn.xiao.permission_system.enums;


import lombok.Getter;

/**
 * @author beth
 * @data 2019-11-01 18:15
 */
@Getter
public enum ErrorCodeEnum implements IErrorCode {

    UN_KNOWN(-10000, "未知错误"),
    SUCCESS(0, "请求成功"),

    BAD_REQUEST(400, "参数错误"),
    UNAUTHORIZED(401, "未经授权"),
    FORBIDDEN(403, "没有权限"),
    NOT_FOUND(404, "请求的资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方式不支持"),
    INTERNAL_SERVER_ERROR(500, "系统繁忙"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    /** 自定义异常之返回值为空 */
    NullResponseException(5002, "返回空值"),
    ;

    /**
     * 状态枚举
     *
     * @param code	状态码
     * @param msg	提示信息
     */
    private ErrorCodeEnum(final Integer code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    private final Integer code;
    private final String msg;



    /**
     * 根据 code 获取对应错误枚举
     *
     * @param code
     * @return
     */
    public static ErrorCodeEnum valueOf(Integer code) {
        ErrorCodeEnum[] enums = ErrorCodeEnum.values();
        for(ErrorCodeEnum errorCodeEnum : enums) {
            if(errorCodeEnum.getCode().equals(code)) {
                return errorCodeEnum;
            }
        }
        return ErrorCodeEnum.UN_KNOWN;
    }

    /**
     * 返回状态码的字符串形式
     *
     * @return
     */
    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
    }

}

